'''
   author:黎作文
   date:2021/12/14 11:44
   project:长津湖.py
'''
'''
Linux用户管理类
• 基类：实现往linux系统里添加用户

• 子类A：
• 能够查看、修改uid和gid、shell等信息

• 子类B：
• 1.能删除用户
• 2.能删除组

• 子类C(A,B)：
• 1.可以单独新建组
• 2.可以将某个用户的gid修改为新建的组的gid
• 3.加入异常机制，异常的名字需要自己定义
'''
import os

class uadd():
    def add(self,addname):
        if os.system(f"id {addname}"):
            print(f"{addname}已存在")
        else:
            os.system(f"useradd {addname}")
            print(f"已成功为您创建{addname}用户")

class umod(uadd):
    def look(self,modname):
        ss='{print $3,$4,$7}'
        lk=os.popen(f"cat /etc/passwd|awk -F: '$1 ~ /{modname}/ {ss}'").read()
        print(f"{modname}用户的的uid，gid及shell分别为：{lk}")

    def muid(self,modname,uuid):
        os.popen(f"usermod -u $modname $uuid")
        ss='{print $3}'
        new=os.popen(f"cat /etc/passwd|awk -F: '$1 ~ /{modname}/ {ss}'").read()
        print(f"{modname}所需修改的uid为{uuid},现在passwd文档里为{new}")

    def mgid(self,modname,ugid):
        os.popen(f"usermod -g $modname $ugid")
        ss='{print $4}'
        new = os.popen(f"cat /etc/passwd|awk -F: '$1 ~ /{modname}/ {ss}'").read()
        print(f"{modname}所需修改的uid为{ugid},现在passwd文档里为{new}")

    def mshell(self,modname,ushell):
        os.popen(f"usermod -s $modname $ushell")
        ss='{print $7}'
        new = os.popen(f"cat /etc/passwd|awk -F: '$1 ~ /{modname}/ {ss}'").read()
        print(f"{modname}所需修改的uid为{ushell},现在passwd文档里为{new}")

class udel(uadd):
    def udel(self,udelname):
        os.popen(f"userdel -r {udelname}")

    def gdel(self,gdelname):
        os.popen(f"groupdel {gdelname}")

class change(umod,udel):
    def gnew(self,gidname,ucname):
        try:
            os.system(f"groupadd  {gidname}")
            print(f"已为您新建名为{gidname}的组")
            os.system(f"usermod -G {gidname} {ucname}")
            print(f"已将{ucname}的附属组加入{gidname}")
            os.system(f"su {ucname}")
            os.popen(f"newgrp {gidname}")
            print(f"已将{ucname}的所属组变成{gidname}")
        except Exception as e:
            print("操作失败")

uadd.add(root)


'''
回合制对战游戏
• 创建角色：名字、性别、血量、装备
• 创建房间：
    • 角色可加入房间
    • 房间内的人可互打
    • 房间上限2人
• 战斗
    • 装备-防弹衣
    • 血量默认100
    • 正常被攻击-10滴血
    • 有防弹衣-5滴血
'''
import random
#invoker 召唤师
class Invoker():
    blood = 100

    def __init__(self,name,sex,equip):
        self.name = name
        self.sex = sex
        self.equip = equip
        self.room = None

    def attach(self, enemy):  #opponent 对手，竞争者
        fight_method = {5:"A(normal attack)",10: "Q skill", 15: "W skill",20:"E skill",30:"R skill"}
        if self == enemy:
            print("You cann't attact yourself!")
        else:
            if self.room and self.room == enemy.room:
                if self.blood <= 0:
                    print("You were already died,and you cann't attact your enemy!")
                else:
                    if random.choice([0, 1]) == 0:
                        lost_blood = random.choice([5,10, 15,20,30])
                        print(f"{enemy.name} has no equipments")
                    else:
                        lost_blood = int(random.choice([5,10,15, 20,30])) * 0.5
                        print(f"{enemy.name} has used equipments")
                    enemy.blood = enemy.blood - lost_blood
                    print(f"{self.name} uses {fight_method[lost_blood]} attacking {enemy.name} for {lost_blood} blood，{enemy.name} still has {enemy.blood} left!")
                    if enemy.blood <= 0:
                        print(f"{enemy.name}were already died,and you cann't attact him anymore!")
                        exit()
            else:
                print("You can't attack unless you're in the same room")

class Room():

    def __init__(self,name):
        self.name = name
        self.member_list = []

    def add_member(self, invoker):
        if invoker.room == None:
            if len(self.member_list) < 2:
                self.member_list.append(invoker)
                invoker.room = self
                print(f"{invoker.name} successfully entered the room {self.name}")
        else:
            print(f"{invoker.name} has entered the room {invoker.room.name}")

invokers = dict()
rooms = dict()

class Process():
    def create_character(self,name,sex,equip):
        invokers[name] = Invoker(name,sex,equip)

    def create_room(self,name):
        rooms[name] = Room(name)

    def enter_room(self,character_name,room_name):
        print(f"当前角色有{invokers.keys()}，当前房间有{rooms.keys()}")
        rooms[room_name].add_member(invokers[character_name])

    def war(self):
        rooms[]

